package com.aop.cglib.aop;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
/**
 * Created by SliverZhang on 2018/1/14.
 */
public class EatProxy implements MethodInterceptor{
    public Eat getProxy(Class clazz){
        Enhancer enhancer = new Enhancer();
        //设置需要创建子类的类
        enhancer.setSuperclass(Eat.class);
        enhancer.setCallback(this);
        //通过字节码技术动态创建子类实例
        return (Eat)enhancer.create();
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("before ...");
        Object result = methodProxy.invokeSuper(o,objects);
        System.out.println("after  ...");
        return result;
    }
}
